package com.huifaapp.mall.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.base.activity.BaseActivity;
import com.huifaapp.mall.base.basemvp.IPresenter;
import com.huifaapp.mall.bean.WebJDShare;
import com.huifaapp.mall.dialogs.LoadingDialog;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Contig;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.Log;

/**
 * Created by Administrator on 2017-12-07.
 */

public class ShopYouhuiShareActivity extends BaseActivity implements View.OnClickListener{

    private ImageButton imagebt_web_jd_share_back;
    private TextView tv_share_yongjin_price;
    private RadioButton radiobtn_Share_ImageText;
    private RadioButton radiobtn_Share_twovc;
    private RadioButton radiobtn_Share_lianjie;
    private LinearLayout ll_share_ImageText;
    private LinearLayout ll_Share_twovc;
    private LinearLayout ll_Share_lianjie;
    private ImageView iv_web_jd_Share_ImageText;
    private EditText et_web_jd_Share_ImageText_nr;
    private CheckBox cb_Share_Check;
    private ImageView iv_web_jd_Share_twovc;
    private ImageView iv_web_jd_Share_lianjie_image;
    private TextView tv_web_jd_share_lianjie_title;
    private TextView tv_web_jd_share_lianjie_jdprice;
    private TextView tv_jd_Share_wx;
    private TextView tv_jd_Share_wxfriends;
    private TextView tv_jd_Share_QQ;
    private TextView tv_jd_Share_lianjie;
    WebJDShare list;
    LoadingDialog dialog;
    protected int getLayoutId() {
        return R.layout.web_jd_share_activity;
    }

    @Override
    protected void initData(Bundle bundle) {
        dialog = new LoadingDialog(ShopYouhuiShareActivity.this);
        dialog.setContent("正在加载数据");
        dialog.show();
        imagebt_web_jd_share_back= (ImageButton) findViewById(R.id.imagebt_web_jd_share_back);
        imagebt_web_jd_share_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_share_yongjin_price= (TextView) findViewById(R.id.tv_share_yongjin_price);
        radiobtn_Share_ImageText= (RadioButton) findViewById(R.id.radiobtn_Share_ImageText);
        radiobtn_Share_ImageText.setOnClickListener(this);
        ll_share_ImageText= (LinearLayout) findViewById(R.id.ll_share_ImageText);
        radiobtn_Share_twovc= (RadioButton) findViewById(R.id.radiobtn_Share_twovc);
        radiobtn_Share_twovc.setOnClickListener(this);
        ll_Share_twovc= (LinearLayout) findViewById(R.id.ll_Share_twovc);
        radiobtn_Share_lianjie= (RadioButton) findViewById(R.id.radiobtn_Share_lianjie);
        radiobtn_Share_lianjie.setOnClickListener(this);
        ll_Share_lianjie= (LinearLayout)findViewById(R.id.ll_Share_lianjie);
        iv_web_jd_Share_ImageText= (ImageView) findViewById(R.id.iv_web_jd_Share_ImageText);
        et_web_jd_Share_ImageText_nr= (EditText) findViewById(R.id.et_web_jd_Share_ImageText_nr);
        cb_Share_Check= (CheckBox) findViewById(R.id.cb_Share_Check);
        iv_web_jd_Share_twovc= (ImageView) findViewById(R.id.iv_web_jd_Share_twovc);
        iv_web_jd_Share_lianjie_image= (ImageView) findViewById(R.id.iv_web_jd_Share_lianjie_image);
        tv_web_jd_share_lianjie_title= (TextView) findViewById(R.id.tv_web_jd_share_lianjie_title);
        tv_web_jd_share_lianjie_jdprice= (TextView) findViewById(R.id.tv_web_jd_share_lianjie_jdprice);
        tv_jd_Share_wx= (TextView) findViewById(R.id.tv_jd_Share_wx);
        tv_jd_Share_wx.setOnClickListener(this);
        tv_jd_Share_wxfriends= (TextView) findViewById(R.id.tv_jd_Share_wxfriends);
        tv_jd_Share_wxfriends.setOnClickListener(this);
        tv_jd_Share_QQ= (TextView) findViewById(R.id.tv_jd_Share_QQ);
        tv_jd_Share_QQ.setOnClickListener(this);
        tv_jd_Share_lianjie= (TextView) findViewById(R.id.tv_jd_Share_lianjie);
        tv_jd_Share_lianjie.setOnClickListener(this);
        radiobtn_Share_ImageText.setChecked(true);
        radiobtn_Share_twovc.setChecked(false);
        radiobtn_Share_lianjie.setChecked(false);
        cb_Share_Check.setChecked(true);
       LoadWebJDShare();
 }
    private void LoadWebJDShare(){
        String id=getIntent().getStringExtra("id");
        String url = ApiManager.PRODUCT_PRESEND+"?spuId="+id+"&token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(ShopYouhuiShareActivity.this);

        android.util.Log.e("数据", "路径Share--" + url);
        HttpUtils http = new HttpUtils();
        //http://www.jhjvip.cn:8081/market/mobile/jd/index?
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                  testTextView.setText(current + "/" + total);
                Log.e("数据", "数据-------" + current + "/" + total);
            }

            //
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
//                    textView.setText(responseInfo.result);
                try {
                    Log.e("数据123", "数据d-------" + responseInfo.result);
                    Gson gson = new Gson();
                    list = gson.fromJson(responseInfo.result, WebJDShare.class);
                    if(list.getCode().equals("10000")) {
                        tv_share_yongjin_price.setText("¥"+list.getData().getPreMoney());
                        uploadimageview(iv_web_jd_Share_ImageText,list.getData().getImgword().getImage(),null,ShopYouhuiShareActivity.this);
                        et_web_jd_Share_ImageText_nr.setText(list.getData().getImgword().getWord());
                        uploadimageview(iv_web_jd_Share_twovc,list.getData().getPoster(),null,ShopYouhuiShareActivity.this);
                        uploadimageview(iv_web_jd_Share_lianjie_image,list.getData().getLink().getImgUrl(),null,ShopYouhuiShareActivity.this);
                        tv_web_jd_share_lianjie_title.setText(list.getData().getLink().getTitle());
                        tv_web_jd_share_lianjie_jdprice.setText(list.getData().getLink().getContent());
                    }else if(list.getCode().equals("10001")){
                        Intent intent=new Intent(ShopYouhuiShareActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(ShopYouhuiShareActivity.this,list.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }catch (JsonSyntaxException e){
                    Toast.makeText(ShopYouhuiShareActivity.this,"未知错误",Toast.LENGTH_LONG).show();
                }
                dialog.dismiss();
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
            }
        });
    }
    @Override
    protected IPresenter onLoadPresenter() {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.radiobtn_Share_ImageText://分享图文
                ll_share_ImageText.setVisibility(View.VISIBLE);
                ll_Share_twovc.setVisibility(View.GONE);
                ll_Share_lianjie.setVisibility(View.GONE);
                radiobtn_Share_ImageText.setChecked(true);
                radiobtn_Share_twovc.setChecked(false);
                radiobtn_Share_lianjie.setChecked(false);
                cb_Share_Check.setChecked(true);
                cb_Share_Check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(cb_Share_Check.isChecked()){

                        }else{

                        }
                    }
                });
                break;
            case R.id.radiobtn_Share_twovc://分享二维码海报
                ll_share_ImageText.setVisibility(View.GONE);
                ll_Share_twovc.setVisibility(View.VISIBLE);
                ll_Share_lianjie.setVisibility(View.GONE);
                radiobtn_Share_ImageText.setChecked(false);
                radiobtn_Share_twovc.setChecked(true);
                radiobtn_Share_lianjie.setChecked(false);

                break;
            case R.id.radiobtn_Share_lianjie://分享链接
                ll_share_ImageText.setVisibility(View.GONE);
                ll_Share_twovc.setVisibility(View.GONE);
                ll_Share_lianjie.setVisibility(View.VISIBLE);
                radiobtn_Share_ImageText.setChecked(false);
                radiobtn_Share_twovc.setChecked(false);
                radiobtn_Share_lianjie.setChecked(true);
               break;
            case R.id.tv_jd_Share_wx:
                if(radiobtn_Share_ImageText.isChecked()) {
                    if(cb_Share_Check.isChecked()){
                        UMImage image = new UMImage(ShopYouhuiShareActivity.this,list.getData().getImgword().getImage());//网络图片
//                        final UMWeb web = new UMWeb(list.getData().getImageWord().getImage());
                        ClipboardUtils.copyText(list.getData().getImgword().getWord());
                        Toast.makeText(ShopYouhuiShareActivity.this,"   推广链接已复制\n可在微信中直接粘贴",Toast.LENGTH_SHORT).show();
                        shareUrl("1",null, null,image);
                    }else {
//                        ClipboardUtils.copyText(list.getData().getImageWord().getWord());
                        shareUrl("1", list.getData().getImgword().getWord(), null,null);
                    }
                }else if(radiobtn_Share_twovc.isChecked()){
                    UMImage image = new UMImage(ShopYouhuiShareActivity.this,list.getData().getPoster());//网络图片
                    shareUrl("1",null, null,image);
                }else if(radiobtn_Share_lianjie.isChecked()){
                    final UMWeb web = new UMWeb(list.getData().getLink().getUrl());
                    web.setTitle(list.getData().getLink().getTitle());//标题
                    web.setThumb(new UMImage(ShopYouhuiShareActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                    web.setDescription(list.getData().getLink().getContent());//描述
                    shareUrl( "3",null,web,null);
                }
                break;
            case R.id.tv_jd_Share_wxfriends:
                if(radiobtn_Share_ImageText.isChecked()) {
                    if(cb_Share_Check.isChecked()){
                        UMImage image = new UMImage(ShopYouhuiShareActivity.this,list.getData().getImgword().getImage());//网络图片
//                        final UMWeb web = new UMWeb(list.getData().getImageWord().getImage());
                        ClipboardUtils.copyText(list.getData().getImgword().getWord());
                        Toast.makeText(ShopYouhuiShareActivity.this,"   推广链接已复制\n可在微信中直接粘贴",Toast.LENGTH_SHORT).show();
                        shareUrl("2",null, null,image);
                    }else {
//                        ClipboardUtils.copyText(list.getData().getImageWord().getWord());
                        shareUrl("2", list.getData().getImgword().getWord(), null,null);
                    }
                }else if(radiobtn_Share_twovc.isChecked()){
                    UMImage image = new UMImage(ShopYouhuiShareActivity.this,list.getData().getPoster());//网络图片
                    shareUrl("2",null, null,image);
                }else if(radiobtn_Share_lianjie.isChecked()){
                    final UMWeb web = new UMWeb(list.getData().getLink().getUrl());
                    web.setTitle(list.getData().getLink().getTitle());//标题
                    web.setThumb(new UMImage(ShopYouhuiShareActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                    web.setDescription(list.getData().getLink().getContent());//描述
                    shareUrl( "4",null,web,null);
                }
                break;
//            case R.id.tv_jd_Share_QQ:
//
//                break;
            case R.id.tv_jd_Share_lianjie:
                ClipboardUtils.copyText(list.getData().getLink().getUrl());
                Toast.makeText(ShopYouhuiShareActivity.this,"   推广链接已复制\n可在微信中直接粘贴",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    /**
     * 分享链接
     */

    private void shareUrl(String type, String con, UMWeb web, UMImage image) {

        android.util.Log.v("shiran","-----------shareUrl()----------");


        UMShareListener umShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
//                if (dialog!=null&&!dialog.isShowing()) {
//                    dialog.show();
//                }
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享成功");
                    Contig.shar=1;
                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享成功");
                    Contig.shar=1;
                } else {
                    ToastUtils.showShortSafe("分享成功");
                    Contig.shar=1;

                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享失败");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享失败");
                } else {
                    ToastUtils.showShortSafe("分享失败");

                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享取消");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享取消");
                } else {
                    ToastUtils.showShortSafe("分享取消");

                }
            }
        };

        if ("3".equals(type)) {  //分享微信
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.WEIXIN).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();

        } else if ("4".equals(type)) { //分享朋友圈

                new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                        setCallback(umShareListener)
                        .withMedia(web)
                        .share();


        }else if ("1".equals(type)) {  //分享微信
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.WEIXIN).
                    setCallback(umShareListener)
                    .withText(con)
                    .withMedia(image)
                    .share();

        } else {
            if ("2".equals(type)) { //分享朋友圈

                new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                        setCallback(umShareListener)
                        .withMedia(image)
                        .withText(con)
                        .share();

            }
        }
    }

    public static void uploadimageview(final ImageView views, String url, String bili, Context c) {
        DisplayImageOptions HXOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoader.getInstance().displayImage(url, views, HXOptions);
    }
}
